import 'package:eleapp/controller/usercontroller.dart';
import 'package:eleapp/res/strings.dart';
import 'package:eleapp/util/save/sp_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../util/locale_util.dart';

class LanguageSettingPage extends StatefulWidget {
  const LanguageSettingPage({super.key});

  @override
  State<LanguageSettingPage> createState() => _LanguageSettingPageState();
}

class _LanguageSettingPageState extends State<LanguageSettingPage> {
  ///新增语言要在此处手动增加
  var languageList = [
    Language(StringStyles.followerSystemLanguage.tr, '', ''),
    Language('简体中文', 'zh', 'CN'),
    Language('繁體中文(香港)', 'zh', 'HK'),
    Language('繁體中文(台灣)', 'zh', 'TW'),
    Language('English', 'en', 'US'),
  ];

  String curLangConntry = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // 利用城市字符串判断当前语言的勾选
    curLangConntry =
        SpUtil.getLanguage() == null ? '' : SpUtil.getLanguage()!.country;
  }

  @override
  Widget build(BuildContext context) {
    SystemController systemController = Get.find();

    return Scaffold(
      // backgroundColor: const Color(0xFFF7F9FF),
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          StringStyles.languageSetting.tr,
          // style: const TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.blue,
        // leading: const CupertinoNavigationBarBackButton(color: Colors.black),
      ),
      body: Container(
        padding: const EdgeInsets.only(
          top: 10,
        ),
        child: ListView.separated(
            itemBuilder: (context, index) {
              return Container(
                padding: const EdgeInsets.only(left: 16, right: 16),
                // color: Colors.white,
                height: 50,
                child: InkWell(
                  onTap: () {
                    // 系统语言的切换
                    LocaleOptions.updateLocale(languageList[index]);
                    // 语言类型缓存到本地
                    SpUtil.updateLanguage(languageList[index]);
                    // 更新勾选图标位置
                    setState(() {
                      curLangConntry = languageList[index].country;
                    });

                    // 修改系统语言后 更新主题提示词
                    int themeindex = SpUtil.getThemeIndex();
                    systemController.updateThemeName(themeindex);

                    // 返回上一页面 没有的话“随系统设置语言”自动切换语言不生效
                    Get.back(result: index);
                  },
                  child: Row(
                    children: [
                      Expanded(child: Text(languageList[index].name)),
                      Visibility(
                          // 利用城市字符串判断当前语言的勾选
                          visible:
                              curLangConntry == languageList[index].country,
                          child: const Icon(Icons.check_rounded,
                              color: Colors.blue))
                    ],
                  ),
                ),
              );
            },
            separatorBuilder: (context, index) {
              return Container(
                height: 1,
                color: Colors.black26,
              );
            },
            itemCount: languageList.length),
      ),
    );
  }
}
